<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>卡密生成</title>
    <link rel="stylesheet" href="__LAYUI__/css/layui.css">
</head>

<body>
    <div class="layui-container" style="margin-top: 15px;">
        <div class="layui-card">
            <div class="layui-card-header">生成卡密</div>
            <div class="layui-card-body">
                <form class="layui-form" action="" lay-filter="cardForm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">卡密类型</label>
                        <div class="layui-input-inline">
                            <select name="type" lay-verify="required" lay-filter="type">
                                <option value="">请选择类型</option>
                                <option value="1">VIP时长</option>
                                <option value="2">经验值</option>
                                <option value="3">金币</option>
                                <option value="4">邀请码</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">卡密值</label>
                        <div class="layui-input-inline">
                            <input type="number" name="value" lay-verify="number" placeholder="请输入卡密值(无则无需填)"
                                class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux" id="valueUnit">选择类型后显示单位</div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">生成数量</label>
                        <div class="layui-input-inline">
                            <input type="number" name="count" lay-verify="required|number|count" value="1"
                                placeholder="请输入生成数量" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">最多一次生成100张</div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="cardSubmit">立即生成</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

    </div>

    <script src="__LAYUI__/layui.js"></script>
    <script>
        layui.use(['form', 'layer', 'util'], function () {
            var form = layui.form,
                layer = layui.layer,
                util = layui.util,
                $ = layui.$;

            // 表单验证规则
            form.verify({
                count: function (value) {
                    if (value > 100) {
                        return '一次最多生成100张卡密';
                    }
                }
            });

            // 监听卡密类型选择
            form.on('select(type)', function (data) {
                var unit = '';
                switch (data.value) {
                    case '1':
                        unit = '单位：天';
                        break;
                    case '2':
                        unit = '单位：点';
                        break;
                    case '3':
                        unit = '单位：个';
                        break;
                }
                $('#valueUnit').html(unit);
            });

            // 监听卡密生成提交
            form.on('submit(cardSubmit)', function (data) {
                layer.load();
                $.ajax({
                    url: '{:url("/admin/fake/doAdd")}',
                    type: 'POST',
                    data: data.field,
                    success: function (res) {
                        layer.closeAll('loading');
                        if (res.code == 200) {
                            layer.msg('生成成功', { icon: 1 });
                            // 重置表单
                            $('form[lay-filter="cardForm"]')[0].reset();
                            form.render();
                        } else {
                            layer.msg(res.msg, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.closeAll('loading');
                        layer.msg('服务器错误', { icon: 2 });
                    }
                });
                return false;
            });

            // 获取类型名称
            function getTypeName(type) {
                switch (type) {
                    case 1: return 'VIP时长';
                    case 2: return '经验值';
                    case 3: return '金币';
                    default: return '未知';
                }
            }
        });
    </script>
</body>

</html>